package com.zhentao.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zhentao.pojo.Services;
import com.zhentao.service.ServicesService;
import com.zhentao.util.Result;
import com.zhentao.vo.ServiceVO;
import jakarta.annotation.Resource;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 服务项目控制器
 */
@RestController
@RequestMapping("/services")
public class ServicesController {

    @Resource
    private ServicesService servicesService;

    /**
     * 获取服务详情（包含类别信息）
     */
    @RequestMapping("/{id}")
    public Result getServiceDetail(@PathVariable Long id) {
        ServiceVO service = servicesService.getServiceWithCategory(id);
        return Result.OK(service);
    }

    /**
     * 分页查询服务列表（包含类别信息）
     */
    @RequestMapping("/page")
    public Result pageServices(@RequestBody com.zhentao.pojo.Page page) {
        Page<Services> page1 = new Page<>(page.getPageNum(), page.getPageSize());
        QueryWrapper<Services> queryWrapper=new QueryWrapper<>();
        Page<Services> result = servicesService.page(page1,queryWrapper);
        return Result.OK(result);
    }

    /**
     * 获取指定类别下的所有服务
     */
    @RequestMapping("/category/{categoryId}")
    public Result getServicesByCategory(@PathVariable Long categoryId) {
        List<ServiceVO> services = servicesService.listServicesByCategoryId(categoryId);
        return Result.OK(services);
    }

    /**
     * 添加服务
     */
    @RequestMapping("/add")
    public Result addService(@RequestBody Services service) {
        servicesService.save(service);
        return Result.OK(service);
    }

    /**
     * 更新服务
     */
    @RequestMapping("/upd")
    public Result updateService(@RequestBody Services service) {
        return Result.OK(servicesService.updateById(service));
    }

    /**
     * 删除服务
     */
    @RequestMapping("/delete")
    public Result deleteService(@PathVariable Long id) {
        return Result.OK(servicesService.removeById(id));
    }
}
